var centerTabs;
var tabsMenu;
$.canDelete = true;
$.canEdit = true;
var treeGrid;
$(function(){
	treeGrid = $('#dg').datagrid({   
		url:'/menu/list',
		method:"get",
	    columns:[[   
	        {field:'menuCode',title:'菜单编码',width:100},   
	        {field:'menuName',title:'菜单名称',width:100},
	        {field:'parent',title:'父菜单ID',width:100},
	        {field:'url',title:'URL',width:100},
	        {field:'action',title:'操作',width:100,
	        	formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="编辑"/>', row.id, '/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;&nbsp;&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '/images/extjs_icons/cancel.png');
					}
					return str;
				}
	        }
	    ]]   
	});
});
//修改
function editFun(id){
	if (id != undefined) {
		treeGrid.treegrid('select', id);
	}
	var node = treeGrid.treegrid('getSelected');
	menuId = id;
	if (node) {
		$.modalDialog({   
		    title: '编辑菜单',   
		    width: 400,   
		    height: 260,
		    closed: false,   
		    cache: false, 
		    href: '/views/menu/menuEdit.html?id='+id,
		    buttons : [ {
				text : '修改',
				handler : function() {
					$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
					var f = $.modalDialog.handler.find('#ff');
					f.submit();
				}
			} ,{
				text : '取消',
				handler : function(){
					$.modalDialog.handler.dialog('close');
				}
				
			}]
		});   
	}
}
//删除
function deleteFun(id) {
	if (id != undefined) {
		treeGrid.treegrid('select', id);
	}
	var node = treeGrid.treegrid('getSelected');
	if (node) {
		$.messager.confirm('询问', '您是否要删除当前资源？', function(b) {
			if (b) {
				$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.ajax({
					url: '/menu/_/'+id,
					type:'delete',
					success: function(data){
						if (data) {
							$.messager.progress('close');
							$.messager.alert('提示',data,'info');
							$('#dg').datagrid('reload'); 
						}else{
							$.messager.alert('提示',data,'error');
						}
					}
				});
			}
		});
	}
}

		